<template>
  <a-modal
    :title="formInfo.id==undefined?'新增':'修改'"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :destroyOnClose="true"
    @ok="handleSubmit"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model
        ref="ruleForm"
        :model="formInfo"
        :label-col="$global.labelCol"
        :wrapper-col="$global.wrapperCol"
      >
            <a-row>
              <a-col :span="20">
            <a-form-model-item label="科室名称" prop="departName" :rules="$valid.required" :maxLength="50">
              <a-input v-model="formInfo.departName" placeholder="请输入科室名称"/>
            </a-form-model-item>
          </a-col>
            </a-row>
        <a-row>
                  <a-col :span="20">
            <a-form-model-item label="科室类型" prop="departType" :rules="$valid.required" :maxLength="50">
              <a-select v-model="formInfo.departType" placeholder="请选择科室类型">
                <a-select-option v-for="(item) in this.$sysDictOptions('departType')" :key="item.k" :value="item.k">
                  {{ item.v }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
            </a-row>
                <a-row>
              <a-col :span="20">
            <a-form-model-item label="排队标记" prop="isQueue" :rules="$valid.required" :maxLength="50">
              <a-select v-model="formInfo.isQueue" placeholder="请选择科排队标记">
                <a-select-option v-for="(item) in this.$sysDictOptions('whetherFlag')":key="item.k" :value="item.k">
                  {{ item.v }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
                </a-row>
        <a-row>
          <a-col :span="20">
            <a-form-model-item label="所属院区" prop="belongArea" :rules="$valid.required" :maxLength="50">
              <a-select v-model="formInfo.belongArea" placeholder="请选择院区">
                <a-select-option v-for="(item) in this.arealist" :key="item.id" :value="item.id">
                  {{item.area_name}}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="20">
            <a-form-model-item label="科室位置" prop="departAddress"  :maxLength="50">
              <a-input v-model="formInfo.departAddress" placeholder="请输入科室位置"/>
            </a-form-model-item>
          </a-col>
        </a-row>
          </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
  import { getAction, postAction } from '@/api/manage'
  import TagSelectOption from "@/components/TagSelect/TagSelectOption";
  import {getArealist} from "@/api/api";
  export default {
    name: 'HealthDepartEdit',
    components: {
      TagSelectOption
    },
    data () {
      return {
        visible: false,
        confirmLoading: false,
        arealist: [],
        formInfo: {
          departName: undefined,
          departType: undefined,
          isQueue: undefined,
          departAddress: undefined,
          belongArea: undefined,
          createTime: undefined,
          wx: ''
        },
        url: {
          saveInfo: '/system/healthDepart/save',
          getInfo: '/system/healthDepart/info'
        }
      }
    },
    created () {

    },
    methods: {
      add () {
        this.edit()
      },
      edit (record) {
        this.$objToOverride(this.formInfo, record)
        if (this.formInfo.id) {
          getAction(this.url.getInfo, { 'id': record.id }).then(res => {
            this.$nextTick(() => {
              this.formInfo = res.result.data
            })
          })
        }
        this.getAreaList()
        this.visible = true
      },
      // 确定
      handleSubmit () {
        // 触发表单验证
        this.confirmLoading = true
        this.$refs.ruleForm.validate(valid => {
          console.log(valid)
          if (valid) {
            postAction(this.url.saveInfo, this.formInfo).then(res => {
              this.$message.success(res.content)
              this.$emit('ok', this.formInfo)
              this.close()
            }).finally(() => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      // 关闭
      handleCancel () {
        this.close()
      },
      close () {
        this.$refs.ruleForm.resetFields()
        this.$objToReset(this.formInfo)
        this.$emit('close')
        this.visible = false
      },
      getAreaList(){
        getArealist({ }).then((res) => {
          const data = res.result.data
          this.arealist = data
        })
      },
      editorOk (val) {
        this.formInfo.content = val
      }
    }
  }
</script>
